package cn.tedu.net.collection.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;

//本类用于测试ArrayList的相关内容
public class Test5_ArrayList {
    public static void main(String[] args) {
        ArrayList<Integer> list=new ArrayList<>();
        list.add(100);
        list.add(200);
        list.add(300);
        list.add(400);
        list.add(300);
        list.add(200);
        list.add(0,666);
        System.out.println(list);
        System.out.println(list.contains(200));
        System.out.println(list.get(0));
        System.out.println(list.indexOf(300));
        System.out.println(list.lastIndexOf(300));
        System.out.println(list.isEmpty());
        System.out.println(list.remove(0));
        System.out.println(list.remove((Integer) 300));
        System.out.println(list.set(2,999));
        System.out.println(list);
        System.out.println(list.size());
        System.out.println(Arrays.toString(list.toArray()));

        /*集合的4种迭代方式*/
        //方式一:for循环
        System.out.println("我是方式一");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");
        }
        System.out.println();
        //方式二:增强for循环:
        System.out.println("我是方式二");
        for (Integer integer : list) {
            System.out.print(integer+" ");
        }
        System.out.println();
        //方式三:Iiterator
        System.out.println("我是方式三");
        Iterator<Integer> list1=list.iterator();
        while (list1.hasNext()){
            System.out.print(list1.next()+" ");
        }
        System.out.println();
        //方式四:ListIterator
        System.out.println("我是方式四");
        ListIterator<Integer> it=list.listIterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();

    }
}
